/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.css.selectors
{

import com.bit101.components.List;

import ru.goodcore.css.datastructure.LinkedList;
import ru.goodcore.css.datastructure.LinkedListNode;
import ru.goodcore.datastructure.DoubleLinkedList;
import ru.goodcore.datastructure.IIterator;
import ru.goodcore.text.StyleTable;

/**
 *  @desc 				A selector is a chain of one or more sequences of 
 * 						simple selectors separated by combinators.
 * 
 *  @author				Dimarik
 *  @version			1.0
 *  @langversion		3.0
 *  @playerversion		9.0
 */

public class SimpleSelectorChain extends LinkedList {

	//-------------------------------------------------------------------------
	//
	//   Class constants
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor 
	 */
	public function SimpleSelectorChain() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  styleTable
	//---------------------------------
	/**
	 * @private
	 */
	private const _styleTable:	StyleTable = new StyleTable();

	public function get styleTable():StyleTable {
		return this._styleTable;
	}

	//---------------------------------
	//  specificity
	//---------------------------------
	/**
	 * @private
	 */
	private var _specificity:	uint;

	public function get specificity():uint {
		return this._specificity;
	}

	//-------------------------------------------------------------------------
	//
	//   Variables & const
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Protected methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Overridden methods: LinkedList
	//
	//-------------------------------------------------------------------------

	public override function append(...args):uint {
		const len:	uint = (args as Array).length;

		var value:	LinkedListNode;
		var temp:	uint;

		for (var i:int = 0; i < len; i++) {

			value = args[i] as LinkedListNode;

			if ( !value ) {
				(args as Array).splice(i, 1);
				i--;
				continue;
			}

			switch (true) {
				case value is IDSelector: // <AA>BBBCCC
					temp = (this._specificity >> 24) + 1;
					this._specificity = (this._specificity & 0x00FFFFFF) | (temp << 24);
					break;

				case value is AttributeSelector: // AA<BBB>CCC
				case value is PseudoClass:
					temp = ((this._specificity >> 12) &  0x00000FFF) + 1;
					this._specificity = (this._specificity & 0xFF000FFF) | (temp << 12);
					break;

				case value is TypeSelector: // AABBB<CCC>
					temp = ((this._specificity) &  0x00000FFF) + 1;
					this._specificity = (this._specificity & 0xFFFFF000) | temp;
					break;
			}
		}

		return super.append.apply(this, args);		
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

}
}
